var express = require('express')
var router = express.Router();

var pool = require('../config/mysqlconfig')
// http://localhost:3000/article/all
router.get('/all',function(req,res,next){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            var sql = "select * from article"
            conn.query(sql,function(error,results){
                if(error){
                    console.log("查询失败")
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

//按名称查询
// http://localhost:3000/article/select
router.post('/select',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            let name = req.body.a_title
            let sql ="select * from article where a_title = ?"
            conn.query(sql,name,function(err,results){
                if(err){
                    console.log(err)
                }
                if(results){
                    res.send(results)
                }
                conn.release()

            })
        }
    })
})
//添加文章
// http://localhost:3000/article/add
router.post('/add',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            // let id = req.body.a_id;
            let title = req.body.a_title;
            let desc = req.body.a_desc;
            let content=req.body.a_content;
            let cate=req.body.a_cate;
            let create=new Date();
            let update=new Date();

            let sql = "insert into article set ?"
            let data = {
                // a_id:id,
                a_title:title,
                a_desc:desc,
                a_content:content,
                a_cate:cate,
                a_createat:create,
                a_updatedat:update
            }
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log("数据库连接失败")
                    return
                }
                if(results){
                    // res.redirect('all')
                    res.send(results)
                }
                conn.release()
            })
        }
    })
})

// 删除文章
// http:localhost:3000/article/del
router.delete('/del',function(req,res){
    
    pool.getConnection(function(err,conn){
        if(err){
            console.log('数据库连接失败')
            
        }else{
            let aid=req.body.id;
            // console.log('id:'+aid)
            let sql='delete from article where id=?'
            conn.query(sql,aid,function(error,results){
                if(error){
                    console.log(err)
                    return
                }
                if(results){
                    // res.send('删除成功')
                    res.send(results)
                    // res.redirect('all')
                    conn.release()
                    
                }
            })
        }
    })
})


// http://localhost:3000/article/update
router.put('/update',function(req,res){

    pool.getConnection(function(err,conn){
        if(err){
            console.log('数据库连接失败')
            return
        }else{
            let id=req.body.id;
            let title=req.body.a_title;
            let desc=req.body.a_desc;
            let content=req.body.a_content;
            let cate=req.body.a_cate;
            
            //更新的记录
            let data=[title,desc,content,cate,new Date(),new Date(),id]
            // sql更新语句
            let sql='update article set a_title=?,a_desc=?,a_content=?,a_cate=?,a_createat=?,a_updatedat=? where id=?'
            // 调用查询方法，执行sql语句
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log(error)
                    return
                }
                if(results){
                    // res.redirect('all')
                    console.log(results)
                    res.send(results)
                    conn.release()
                }
            })
        }
    })  
})

module.exports = router